<?php

namespace App\Admin\Controllers;

use SmallRuralDog\Admin\Components\Antv\Area;
use SmallRuralDog\Admin\Components\Antv\Column;
use SmallRuralDog\Admin\Components\Antv\Line;
use SmallRuralDog\Admin\Components\Antv\StepLine;
use SmallRuralDog\Admin\Components\Widgets\Alert;
use SmallRuralDog\Admin\Components\Widgets\Card;
use SmallRuralDog\Admin\Controllers\AdminController;
use SmallRuralDog\Admin\Layout\Content;
use SmallRuralDog\Admin\Layout\Row;
use SmallRuralDog\Admin\Form;
use SmallRuralDog\Admin\Grid;
use SmallRuralDog\Admin\Components\Form\Input;
use SmallRuralDog\Admin\Components\Widgets\Html;
use App\Models\Order;
use App\Models\ActivityEnter;
use App\Http\Controllers\Controller;
use SmallRuralDog\Admin\Components\Widgets\BaseForm;
use SmallRuralDog\Admin\Components\Form\DatePicker;
use SmallRuralDog\Admin\Grid\Actions\ActionButton;
use SmallRuralDog\Admin\Grid\Tools\ToolButton;
use Illuminate\Http\Request;


use SmallRuralDog\Admin\Components\Widgets\Button;
use SmallRuralDog\Admin\Components\Widgets\Dialog;
use SmallRuralDog\Admin\Components\Widgets\Markdown;

class HomeController extends AdminController
{
    public function index(Content $content)
    {
        $content->className('m-10')
            ->row(function (Row $row) {
                $row->gutter(10);
                $row->column(24, Alert::make("你好！！", "欢迎使用恩众服B2B系统")->showIcon()->closable(false)->type("success"));
            })->row(function (Row $row) {
                $row->gutter(10)->className('mt-10');
                $row->column(24, Card::make()->bodyStyle(['padding' => '0'])->content(
                    Line::make()
                        ->data(function () {
                            $data = collect();
                            for ($year = 2010; $year <= 2020; $year++) {
                                $data->push([
                                    'year' => (string)$year,
                                    'type' => '小红2',
                                    'value' => rand(100, 1000)
                                ]);
                                $data->push([
                                    'year' => (string)$year,
                                    'type' => '小白',
                                    'value' => rand(100, 1000)
                                ]);
                            }
                            return $data;
                        })
                        ->config(function () {
                            return [
                                'title' => [
                                    'visible' => true,
                                    'text' => '活动统计',
                                ],
                                'description' => [
                                    'visible' => true,
                                    'text' => '活动参与情况统计',
                                ],
                                'seriesField' => 'type',
                                'smooth' => true,
                                'xField' => 'year',
                                'yField' => 'value'
                            ];
                        })
                ));

            })->row(function (Row $row) {
                $row->gutter(10)->className('mt-10');
                $html = '';
                $row->column(24, Card::make()->bodyStyle(['padding' => '0'])->header('sdfds')->content(
                    Column::make()
                        ->data(function () {
                            $data = collect();
                            $result = Order::getOrderNum('month');
                            foreach($result['label'] as $key=>$val)
                            {
                                 $data->push([
                                    'year' => (string)$val,
                                    'type' => '订单数量',
                                    // 'value' => intval($result['report'][$val]),
                                    'value' => rand(100, 1000),
                                ]);
                            }

                            return $data;
                        })
                        ->config(function () {
                            return [
                                'title' => [
                                    'visible' => false,
                                    'text' => '订单统计',
                                ],
                                'description' => [
                                    'visible' => true,
                                    'text' => '订单兑换数量统计',
                                ],
                                'smooth' => false,
                                'xField' => 'year',
                                'yField' => 'value'
                            ];
                        })
                ));
            });
        return $content;
    }

    public function home(Content $content, Request $request)
    {
        if($request->isMethod('post') && $request->all()){
            $params = $request->all();

            $url = env('APP_URL').'/admin#/home?'.http_build_query($params);

            header('Location:'. $url);

        }

        // $content->row(function (Row $row) {
        //         $row->item(Button::make("路由跳转")->handler(Button::HANDLER_ROUTE)->uri('/home')->tooltip("跳转到首页"));
        //         $row->item(Button::make("链接跳转")->handler(Button::HANDLER_LINK)->uri('https://laravel-vue-admin.com')->message("是否要跳转？"));
        //     });

        // $this->filter = $request->all();
        // dd($this->filter);
        $content->className('m-10')
            ->row(function (Row $row) {
                $row->gutter(10);
                $row->column(24, Alert::make("你好！！", "欢迎使用恩众服B2B系统")->showIcon()->closable(false)->type("success"));
            })->row(function (Row $row) {
                $row->gutter(10)->className('mt-10');
                $row->column(24, Card::make()->bodyStyle(['padding' => '0'])->content(
                    Line::make()
                        ->data(function () {
                            $data = collect();
                            for ($year = 2010; $year <= 2020; $year++) {
                                $data->push([
                                    'year' => (string)$year,
                                    'type' => '小红',
                                    'value' => rand(100, 1000)
                                ]);
                                $data->push([
                                    'year' => (string)$year,
                                    'type' => '小白',
                                    'value' => rand(100, 1000)
                                ]);
                            }
                            return $data;
                        })
                        ->config(function () {
                            return [
                                'title' => [
                                    'visible' => true,
                                    'text' => '活动统计',
                                ],
                                'description' => [
                                    'visible' => true,
                                    'text' => '活动参与情况统计',
                                ],
                                'seriesField' => 'type',
                                'smooth' => true,
                                'xField' => 'year',
                                'yField' => 'value'
                            ];
                        })
                ));

            })->row(function (Row $row) {
                $row->gutter(10)->className('mt-10');
                // $row->column(24, Card::make()->content( function (Content $content) use ($row) {
                //     $baseForm = new BaseForm();
                //     $baseForm->action(route("admin.home"));
                //     // $baseForm->emit("sdf");
                //     $baseForm->emit("tableReload");
                //     $baseForm->addValue('type', 'orderCount');
                //     $baseForm->item("time", "选择时间")->component(DatePicker::make(null, 'datetimerange'));
                //     $content->row($baseForm);
                // }));

                // dd($this->filter);
                $data  = function ()  {
                    $data = collect();
                    if (isset($this->filter['time']))
                    {
                        $type = 'custome';
                        $filter = [
                            'start_time' => $this->filter['time'][0],
                            'end_time' => $this->filter['time'][1],
                        ];
                    }
                    else
                    {
                       $type = 'month';
                       $filter = [];
                    }
                    $result = Order::getOrderNum($type, $filter);
                    foreach($result['label'] as $key=>$val)
                    {
                         $data->push([
                            'month' => (string)$val,
                            'type' => '订单数量',
                            // 'value' => intval($result['report'][$val]),
                            'value' => rand(100, 1000),
                        ]);
                    }
                    // dd($content);
                    return $data;
                };
                $config = [
                    'title' => [
                        'visible' => false,
                        'text' => '订单统计',
                    ],
                    'description' => [
                        'visible' => true,
                        'text' => '订单数量统计',
                    ],
                    'smooth' => false,
                    'xField' => 'month',
                    'yField' => 'value'
                ];
                $row->column(24, Card::make()->bodyStyle(['padding' => '0'])->header(function (Content $content) use ($row) {
                    // $baseForm = new BaseForm();
                    // $baseForm->action(route("admin.home"));
                    // $baseForm->emit("tableReload");
                    // $baseForm->addValue('type', 'getOrderNum');

                    // $baseForm->item("time", "选择时间")->component(DatePicker::make(['2020-07-10','2020-08-31'], 'datetimerange'));

                    // $content->row($baseForm);

                })->content(
                    Column::make()
                        ->data($data)
                        ->config($config)
                ));
            });
        return $content;
    }

}

